import React, { Component } from 'react';
import { withRouter } from 'react-router'

import './style.css';
import JudgeTable from '../../components/JudgeTable';

import JudgeLocalStorage from '../../config/JudgeLocalStorage';

import PrintTemplate from 'react-print';
// import printJS from 'print.js/src';

import sgdwny from '../../json/sgdw-ny.json';
import sgdwwyty from '../../json/sgdw-wyty.json';
import sgdwglgc from '../../json/sgdw-glgc.json';
import sgdwsygc from '../../json/sgdw-sygc.json';
import jl from '../../json/jl.json';
import jsdw from '../../json/jsdw.json';
const judgeDateMap = {
  '内业考核': { name: "表1 施工单位内业部分考评价表", data: sgdwny, tableType: 'ny' },
  '外业通用': { name: "表2.1 施工外业通用部分考核评价表", data: sgdwwyty, tableType: 'wy' },
  '公路工程': { name: "表2.2 施工外业公路工程考核评价表", data: sgdwglgc, tableType: 'wy' },
  '水路工程': { name: "表2.1 施工外业水路工程考核评价表", data: sgdwsygc, tableType: 'wy' },
  '监理单位考核评价': { name: "表3 监理单位考核评价", data: jl, tableType: 'ny' },
  "建设单位考核评价": { name: "表4 建设单位考核评价", data: jsdw, tableType: 'ny' }
};

class Judge extends Component {
  constructor(props) {
    super(props);

    const { projectId, canMark } = this.props.location.state;

    const projectDate = JudgeLocalStorage.getItem('projects')[projectId];
    console.log("考核标准信息", projectId, projectDate);
    const { judgeClassify } = projectDate;

    const judgeTables = {};

    //根据考核项目设置的考核表类型 获取对应考核表的考试原始数据
    judgeClassify.map(item => {
        console.log(item);
        judgeTables[item] = judgeDateMap[item];
        return null;
      }
    );

    /**
     * 根据考核项目ID获取此次考核的得分数据，如果没有获取到，则构建评分数据数据结构为
     * {"poeject-时间戳':{
     *  '内页考核':{
     *      '责任落实':{
     *          standardScore:10 标准分
     *          isJudge:true,是否参与考核
     *          markScore: 评分
     *          description: 描述
     *          selectMarkStandard:["",""] 选择的扣分标准
     *        }
     *    },
     *    '外业通用':{
     *    }
     * }}
     **/
    const markScoresDB = JudgeLocalStorage.getItem('markScores');
    let markScores = {};
    if (!markScoresDB || !markScoresDB[projectId]) {
      //构建评分数据
      Object.keys(judgeTables).map(key => {
        let judgeTableDate = judgeTables[key].data;

        let judgeClassifyScores = {};

        if (judgeTables[key].tableType === 'ny') {
          judgeTableDate.map((item) => {
            let scoreItem = {};
            scoreItem['standardScore'] = item.standardScore;
            scoreItem['markScore'] = 0;
            scoreItem['description'] = '';
            scoreItem['selectMarkStandard'] = [];
            scoreItem['isJudge'] = true;
            judgeClassifyScores[item.name] = scoreItem;
            return null;
          });
        } else {
          judgeTableDate.map((item) => {
            item.children.map(child => {
              let scoreItem = {};
              scoreItem['standardScore'] = child.standardScore;
              scoreItem['markScore'] = 0;
              scoreItem['description'] = '';
              scoreItem['selectMarkStandard'] = [];
              scoreItem['isJudge'] = true;
              judgeClassifyScores[child.name] = scoreItem;
              return null;
            });
            return null;
          });
        }
        markScores[key] = judgeClassifyScores;
        return null;
      });
    } else {
      markScores = markScoresDB[projectId];
    }

    console.log('评分结果', projectId, markScoresDB);
    this.state = {
      projectDate,
      judgeTables,
      markScores,
      canMark: canMark
    }
  }



  componentDidMount() {
    window.print();
  }
  render() {

    const { judgeTables } = this.state;


    return <div>
      <button onClick={() => this.props.history.goBack()}>返回</button>
      <PrintTemplate>
          {Object.keys(judgeTables).map(key =>
            <JudgeTable key={key}
                        tableClassify={key}
                        tableType={judgeTables[key].tableType}
                        judgeDate={judgeTables[key].data}
                        judgeTableName={judgeTables[key].name}
                        markScores={this.state.markScores[key]}
                        canMark={false}/>
          )}
      </PrintTemplate>
    </div>
  }
}

export default withRouter(Judge);